<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 网站内容设置
 * @author flyten 
 */

class settingModel extends CI_Model{
  
    
    private $table = 'setting';  
  
    public function __construct() {
      parent::__construct();
    }
     
    
    public  function listA(){
      $query = $this->db->query("select * from $this->table");
      $arrayList = $query->result_array();
      $arrayResult = array();
      foreach( $arrayList as $val ){
        $k = $val['setting_name'];
        $v = $val['setting_value'];
        $arrayResult[$k] = $v;
      }
      return $arrayResult;
    }
    
    
    /**
     * 根据列和值查询一条记录
     * @param type $columnName
     * @param type $columnVar
     * @return type 
     */
    public function getOneByColumn( $columnName, $columnVar = '' ){
      if(!is_string($columnName) || !is_string($columnName) ){
        return array();
      }
      $sql = "SELECT * FROM $this->table WHERE $columnName='$columnVar' LIMIT 1";
      $query = $this->db->query($sql);
      $arrayResult = $query->row_array();
      return $arrayResult;
    }


    /**
     * 插入数据
     * @param type $arrayData
     * @return type 
     */
     public function insert( $arrayData ){
       if(!is_array($arrayData)){
         return 0;
       }
       $arrayKeys = array_keys($arrayData);
       $key = $arrayKeys[0];
       $arrayResult = $this->getOneByColumn( $key, $arrayData[$key]  );
       $flag = 0;
       if(empty($arrayResult)){
         $flag = $this->db->insert($this->table, $arrayData);
       }else{
         $flag = $this->update($arrayResult['id'], $arrayData);
       }
       return $flag;
     }
     
     /**
      * 修改
      * @param type $id
      * @param type $arrayData 
      */
     public function update( $id, $arrayData ){
       $this->db->where('id', $id);
       return $this->db->update($this->table, $arrayData);
     }
     
     public function del( $id ){
       $this->db->where('id', $id);
       $this->db->delete($this->table);
     }
}
